package com.gcsoftware.web.mapper;

import com.gcsoftware.authentications.beans.RoleInfo;
import com.gcsoftware.web.beans.T_GW_INTERFS;
import com.gcsoftware.web.beans.T_GW_ROLES;
import com.gcsoftware.web.mapper.providers.RoleDsiInterfsProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by qs on 2018/3/14.
 */
@Mapper
public interface T_GW_ROLEMAPPER {

    @Select("<script>SELECT C_ROLE_ID ,C_ROLE_NAME ,C_ROLE_REMARK FROM T_GW_ROLES WHERE c_ROLE_TYPE = #{c_ROLE_TYPE} <when test='c_ROLE_NAME != null'>AND c_ROLE_NAME like  CONCAT(CONCAT('%', #{c_ROLE_NAME, jdbcType=VARCHAR}), '%')</when> ORDER BY D_MODIFY_TIME DESC </script>")
    public List<T_GW_ROLES> queryForList(@Param(value = "c_ROLE_TYPE") String c_ROLE_TYPE,@Param(value = "c_ROLE_NAME") String c_ROLE_NAME);

    @Select("SELECT max(C_ROLE_ID) FROM T_GW_ROLES WHERE C_ROLE_TYPE = '0'")
    public String getMaxId();

    @Select("<script>SELECT C_ROLE_ID ,C_ROLE_NAME ,C_ROLE_REMARK FROM T_GW_ROLES WHERE 1 = 1 <when test='C_ROLE_ID != null'>AND C_ROLE_ID = #{C_ROLE_ID, jdbcType=VARCHAR}</when> <when test='c_ROLE_NAME != null'>AND c_ROLE_NAME = #{c_ROLE_NAME, jdbcType=VARCHAR}</when></script>")
    public T_GW_ROLES get(@Param(value = "C_ROLE_ID") String C_ROLE_ID , @Param(value = "c_ROLE_NAME") String c_ROLE_NAME);

    @Insert("INSERT INTO T_GW_ROLES (C_ROLE_ID ,C_ROLE_NAME ,C_ROLE_REMARK) VALUES (#{c_ROLE_ID},#{c_ROLE_NAME},#{c_ROLE_REMARK})")
    public int insert(T_GW_ROLES dsitem);

    @Update("UPDATE T_GW_ROLES SET D_MODIFY_TIME=CURRENT_TIMESTAMP , C_ROLE_NAME= #{c_ROLE_NAME},C_ROLE_REMARK = #{c_ROLE_REMARK} WHERE C_ROLE_ID = #{c_ROLE_ID}")
    public int update(T_GW_ROLES dsitem);

    @Delete("DELETE FROM T_GW_ROLES WHERE C_ROLE_ID = #{c_ROLE_ID}")
    public int delete(@Param(value = "c_ROLE_ID") String c_ROLE_ID);

    @Select("SELECT r.C_ROLE_ID roleid,c_ROLE_VALUE rolename FROM T_GW_ROLES  r , T_COM_SYS_USER u where u.c_STATUS ='USER_STATUS_NORMAL' and u.C_ROLE_ID = r.C_ROLE_ID and u.c_USER_ID =#{userId}")
    public List<RoleInfo> queryUserRoles(String userId);

    @Delete("DELETE T_CON_ROLE_DSITEM WHERE c_ROLE_ID = #{c_ROLE_ID} AND c_INTERF_ID in (${interfs})")
    public int deleteRoleDsiInterfs(@Param(value = "c_ROLE_ID") String c_ROLE_ID,@Param(value = "interfs") String interfs);

    @Select("SELECT c_INTERF_ID FROM T_CON_ROLE_INTERFS WHERE c_ROLE_ID = #{c_ROLE_ID}")
    public List<String> queryInterfListForRole(@Param(value = "c_ROLE_ID") String c_ROLE_ID);

    @Select("SELECT itf.c_INTERF_ID, c_INTERF_METHOD_NAME ,itf.C_INTERF_ENDPOINT ,itf.c_APP_ID,itf.c_INTERF_UNIFY,itf.c_INTERF_SERVICE  FROM T_GW_INTERFS itf ,T_CON_ROLE_INTERFS con where con.C_INTERF_ID  = itf.C_INTERF_ID AND con.c_ROLE_ID = #{c_ROLE_ID}")
    public List<T_GW_INTERFS> queryAccessInterfListForRole(@Param(value = "c_ROLE_ID") String c_ROLE_ID);

    @Select("SELECT c_DS_ITEM_ID FROM T_CON_ROLE_DSITEM WHERE c_ROLE_ID = #{c_ROLE_ID}")
    public List<String> queryDsiListForRole(@Param(value = "c_ROLE_ID") String c_ROLE_ID);

    @InsertProvider(type=RoleDsiInterfsProvider.class,method = "batchInsertInterfs")
    public int batchInsertRoleInterfs(@Param(value = "c_ROLE_ID") String c_ROLE_ID, @Param(value = "interfList") List<String> interfList);

    @Delete("DELETE T_CON_ROLE_INTERFS WHERE c_ROLE_ID = #{c_ROLE_ID}")
    public int removeRoleInters(@Param(value = "c_ROLE_ID") String c_ROLE_ID);
    @Delete("DELETE T_CON_ROLE_DSITEM WHERE c_ROLE_ID = #{c_ROLE_ID}")
    public int removeRoleDsis(@Param(value = "c_ROLE_ID") String c_ROLE_ID);

    @InsertProvider(type=RoleDsiInterfsProvider.class,method = "batchInsertDsis")
    public int batchInsertRoleDsis(@Param(value = "c_ROLE_ID") String c_ROLE_ID, @Param(value = "dsiList") List<String> dsiList);
}
